<?php

/*
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goods extends CI_Controller {

	public $data;
	private $limit = 15;

	public function __construct()
	{
		parent::__construct();
		//$this->load->database();
		//载入必要类文件
		$this->load->model('Login_model');
		$this->load->model('User_model');
		$this->load->model('Right_model');
		$this->load->model('Goods_type_model');
		$this->load->model('Main_goods_model');
		$this->load->model('Sub_goods_model');
		$this->load->model('Supplier_model');
		$this->load->model('Suppliers_Goods_model');
		//载入必要库文件
		$this->load->library('pagination');
		$this->load->library('Access_control');
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['title'] = '货品管理';
		$this->data['styles'] = array('master.css', 'error.css', 'goods.css');
		$this->data['scripts'] = array('master.js', 'goods.js');
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}
		//设置站点基本参数
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['header']['username'] = $this->data['cur_user']['realname'];
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 4);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	private function _menus()
	{
		return array(
			array(
				'index'	=>	1,
				'url'	=>	$this->data['site']['root'] . 'goods/enable/',
				'name'	=>	'查看在售货品',
				'right' => 'goods.enable'
			),
			array(
				'index'	=>	2,
				'url'	=>	$this->data['site']['root'] . 'goods/disable/',
				'name'	=>	'查看未售货品',
				'right' => 'goods.disable'
			),
			array(
				'index'	=>	3,
				'url'	=>	$this->data['site']['root'] . 'goods/create/',
				'name'	=>	'创建货品',
				'right' => 'goods.create'
			),
			array(
<<<<<<< HEAD
				'index' =>	0,
				),
			array(
				'index' =>	5,
				'url'	=>	$this->data['site']['root'] . 'goods/stocks/',
				'name'	=>	'货物库存结算',
				'right' =>  'goods.stocks'
=======
				'index' =>	4,
				'url'	=>	$this->data['site']['root'] . 'goods/Show_table/',
				'name'	=>	'打印宝贝列表',
				'right' => 'goods.show_table'
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
			)
		);
	}

	private function _suppliers()
	{
		$this->db->where('del_flg', 0);
		$this->db->order_by('supplier_priority');
	}

	public function index()
	{
		$this->enable();
	}

	public function create()
	{
		$this->data['sub_menu_index'] = '3';
		if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
		{
			$this->Main_goods_model->create();
			if (!empty($this->Main_goods_model->error))
			{
				$this->data['form']['result'] = $this->Main_goods_model->error;
			}
		}
		$this->data['form']['rand'] = $this->forbid_resubmit->rand_value();
		$this->data['form']['types'] = $this->Goods_type_model->_list('enable');
		return $this->load->view_('main_goods_create_view', $this->data);
	}

	public function update($type, $id)
	{
		if ('main' == $type)
		{
			if ($this->data['form']['main_goods'] = $this->Main_goods_model->getOne($id))
			{
				if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
				{
					unset($this->data['form']['main_goods']['name'], $this->data['form']['main_goods']['taobao_url']);
					if ($this->Main_goods_model->update())
					{
						$this->data['form']['main_goods'] = $this->Main_goods_model->getOne($id);
						$this->data['form']['result'] = '更新成功';
					}
				}
				$this->data['form']['types'] = $this->Goods_type_model->_list('enable');
				$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
				return $this->load->view_('main_goods_update_view', $this->data);
			}
			$this->data['error'] = '拒绝错误';
			return $this->load->view_('error_500_view', $this->data);
		}
		else if ('sub' == $type)
		{
			//判断子货品是否存在
			if ($this->data['form']['sub_goods'] = $this->Sub_goods_model->getOne($id))
			{
				//初始化参数
				$goods_types = $this->Goods_type_model->getAll('enable');	//[array]主货品可用类型
				$mid = $this->data['form']['sub_goods']['main_goods_id'];	//[string]获取主类目id
				$exist_suppliers = $this->Suppliers_Goods_model->get_suppliers($id);	//[Object]当前子货品关联的供应商id
				$this->data['mgoods'] = $this->Main_goods_model->getOne($mid);	//[array]主货品信息
				$this->data['mgoods']['type'] = $goods_types[$this->data['mgoods']['goods_type_id']];	//[string]主货品类目
				$this->data['suppliers'] = $this->Supplier_model->getAll(0);	//[object]可用的供应商
				//过滤供应商
				$this->data['suppliers_info'] = array();
				foreach ($exist_suppliers as $exist_supplier)
				{
					$this->data['suppliers_info'][] = $this->Supplier_model->getOne($exist_supplier->supplier_id);
					foreach ($this->data['suppliers'] as $key=>$supplier)
					{
						if ($exist_supplier->supplier_id == $supplier->supplier_id)
						{
							unset($this->data['suppliers'][$key]);
						}
					}
				}
				//var_dump($this->data['suppliers_info']);
				//unset($exist_suppliers);
				foreach ($this->data['mgoods']['taobao_info'] as $k => $v)
				{
					$this->data['mgoods']['taobao_'.$k] = $v;
					if ('image' == $k && empty($v))
					{
						$this->data['mgoods']['taobao_'.$k] = $this->data['site']['images'] . 'blank.gif';
					}
					else if ('name' == $k && empty($v))
					{
						$this->data['mgoods']['taobao_'.$k] = '暂无';
					}
					else if ('url' == $k && empty($v))
					{
						$this->data['mgoods']['taobao_'.$k] = 'javascript:void(0)';
					}
				}
				if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
				{
					unset($this->data['form']['sub_goods']['attribute']);
					$this->db->trans_strict(FALSE);
					$this->db->trans_start();
					if ($this->Sub_goods_model->update())
					{
						if ('' != $this->input->post('supplier_ids'))
						{
							$this->Suppliers_Goods_model->update($this->input->post('sub_goods_id'));
						}
						header("Location: " . $this->data['site']['root'] . 'goods/update/sub/' . $id);
					}
					else
					{
						$this->data['form']['result'] = '更新失败';
					}
					$this->db->trans_complete();
					$this->data['form']['sub_goods'] = $this->Sub_goods_model->getOne($id);
				}
				//获取当前
				$this->data['sgoods'] = $this->Sub_goods_model->getGoods($mid);
				$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
				return $this->load->view_('sub_goods_update_view', $this->data);
			}
			$this->data['error'] = '访问错误';
			return $this->load->view_('error_500_view', $this->data);
		}
	}

<<<<<<< HEAD
	public function stocks()
	{
		//载入模型
		$this->load->model('Goods_Relation_model');

		//读取当前所有主货物
		$main_goods = $this->Main_goods_model->get_all();

		//获得当前所有子货物
		$sub_goods = $this->Sub_goods_model->get_all();

		//关联主货物和子货物
		$this->Goods_Relation_model->link($main_goods, $sub_goods);
		$this->data['goods'] = $this->Goods_Relation_model->goods();

		$this->load->view('goods_stock_view', $this->data);

	}

=======
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
	public function enable($page = 1)
	{
		$this->data['sub_menu_index'] = '1';
		$this->data['page']['display'] = $this->_list('enable', $page);
		return $this->load->view_('main_goods_list_view', $this->data);
	}

	public function disable($page = 1)
	{
		$this->data['sub_menu_index'] = '2';
		$this->data['page']['display'] = $this->_list('disable', $page);
		return $this->load->view_('main_goods_list_view', $this->data);
	}

	private function _list($opt, $page)
	{
		$start = ($page - 1) * $this->limit;
		$this->data['list']['opt'] = $opt;
		$this->data['main_goods'] = $this->Main_goods_model->getList($opt, $this->limit, $start);
		$this->data['page']['total_rows'] = $this->Main_goods_model->totleRows($opt);
		$this->data['page']['base_url'] = $this->data['site']['root'] . 'goods/' . $opt . '/';
		$this->data['page']['per_page'] = $this->limit;
		$this->data['page']['use_page_numbers'] = TRUE;
		$this->data['page']['display_pages'] = FALSE;
		$this->data['page']['first_tag_open'] = '<div class="hide">';
		$this->data['page']['first_tag_close'] = '</div>';
		$this->data['page']['close_tag_open'] = '<div class="hide">';
		$this->data['page']['close_tag_close'] = '</div>';
		$this->data['page']['next_link'] = '';
		$this->data['page']['next_tag_open'] = '<div id="next" class="btn">';
		$this->data['page']['next_tag_close'] = '</div>';
		$this->data['page']['prev_link'] = '';
		$this->data['page']['prev_tag_open'] = '<div id="prev" class="btn">';
		$this->data['page']['prev_tag_close'] = '</div>';
		$this->pagination->initialize($this->data['page']);
		$this->data['page']['start'] = ($page-1) * $this->limit + 1;
		$this->data['page']['end'] = $this->data['page']['start'] + $this->limit;
		if (0 == $this->data['page']['total_rows']) 
		{
			$this->data['page']['start'] = 0;
		}
		if ($this->data['page']['end'] > $this->data['page']['total_rows'])
		{
			$this->data['page']['end'] = $this->data['page']['total_rows'];
		}
		return $this->pagination->create_links();
	}

	//Ajax 删除/回复主货品
	public function mainGoods($action)
	{
<<<<<<< HEAD
=======
		//权限控制
		if (!$this->access_control->base(102, $this->data['cur_user']['rights']))
		{
			$this->data['ajax']['request'] = 'ERROR';
			$this->data['ajax']['data'] = '无权限操作';
			$this->load->view_('ajax_json', $this->data);
			return;
		}
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151

		$this->db->trans_strict(FALSE);
		//开始事务
		$this->db->trans_start();
		foreach ($this->input->post('main_goods_id') as $v)
		{
			if ('del' == $action)
			{
				$this->Main_goods_model->del($v);
				//$this->Sub_goods_model->del($v, true);
				$this->data['ajax_request'] = 'DEL_SUCCESS';
			}
			else if ('recover' == $action)
			{
				$this->Main_goods_model->recover($v);
				//$this->Sub_goods_model->recover($v, true);
				$this->data['ajax_request'] = 'RECOVER_SUCCESS';
			}
		}
		$this->db->trans_complete();
		return $this->load->view('ajax_request', $this->data);
	}

	//Ajax 删除/恢复子货品
	public function subGoods($action)
	{
<<<<<<< HEAD
=======
		//权限控制
		if (!$this->access_control->base(102, $this->data['cur_user']['rights']))
		{
			$this->data['ajax']['request'] = 'ERROR';
			$this->data['ajax']['data'] = '无权限操作';
			$this->load->view_('ajax_json', $this->data);
			return;
		}
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151

		$this->db->trans_strict(FALSE);
		$this->db->trans_start();
		foreach ($this->input->post('sub_goods_id') as $v)
		{
			if ('del' == $action)
			{
				$this->Sub_goods_model->del($v);
				$this->data['ajax_request'] = 'DEL_SUCCESS';
			}
			else if ('recover' == $action) {
				$this->Sub_goods_model->recover($v);
				$this->data['ajax_request'] = 'RECOVER_SUCCESS';
			}
		}
		$this->db->trans_complete();
		return $this->load->view('ajax_request', $this->data);
	}

	//Sub_goods_model
	public function createSub($mid)
	{
<<<<<<< HEAD
=======
		//访问控制
		$access_id = 101;
		if (!$this->access_control->base($access_id, $this->data['cur_user']['rights']))
		{
			$this->data['error'] = '拒绝访问';
			$this->load->view_('error_500_view', $this->data);
			return false;
		}
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
		//获得初始数据
		$goods_types = $this->Goods_type_model->getAll('enable');			//主货品所有类型
		$this->data['mgoods'] = $this->Main_goods_model->getOne($mid);		//当前主货品数据
		$this->data['suppliers'] = $this->Supplier_model->getAll(0);		//所有可用的供应商
		//判断主货品存在
		if (!$this->data['mgoods'])
		{
			$this->data['error'] = '访问错误';
			return $this->load->view_('error_500_view', $this->data);
		}
		//判断表单提交状态
		if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
		{
			//插入数据库
			$this->db->trans_strict(FALSE);
			$this->db->trans_start();
			if ($this->Sub_goods_model->create($this->data['mgoods']['number'], $this->data['mgoods']['del_flg']))
			{
				$this->Suppliers_Goods_model->update($this->db->insert_id());
			}
			$this->db->trans_complete();
		}
		//组织主货品信息
		$this->data['mgoods']['type'] = $goods_types[$this->data['mgoods']['goods_type_id']];
		foreach ($this->data['mgoods']['taobao_info'] as $k => $v)
		{
			$this->data['mgoods']['taobao_'.$k] = $v;
			if ('image' == $k && empty($v))
			{
				$this->data['mgoods']['taobao_'.$k] = $this->data['site']['images'] . 'blank.gif';
			}
			else if ('name' == $k && empty($v))
			{
				$this->data['mgoods']['taobao_'.$k] = $this->data['mgoods']['taobao_url'];
			}
			else if ('url' == $k && empty($v))
			{
				$this->data['mgoods']['taobao_'.$k] = 'javascript:void(0)';
			}
		}
		//获取当前
		$this->data['sgoods'] = $this->Sub_goods_model->getGoods($mid);
		$this->data['form']['rand'] = $this->forbid_resubmit->rand_value();
		$this->data['form']['types'] = $this->Goods_type_model->_list('enable');
		return $this->load->view_('sub_goods_create_view', $this->data);
	}

	public function getSubGoods()
	{
		$this->data['ajax']['data'] = $this->Sub_goods_model->getGoods();
		$this->data['ajax']['request'] = 'GET_SUCCESS';
		return $this->load->view('ajax_json', $this->data);
	}

	public function search($type, $search, $page = 1)
	{
		//添加菜单
		$this->data['sub_menus'] = $this->menu_control->add_sub($this->data['sub_menus'], array('index'=>101,'url'=>'javascript:void(0)','name'=>'搜索结果'), true);
		$this->data['sub_menu_index'] = 101;
		$this->data['search_text'] = $type . ':' . $search;
		switch ($type)
		{
			case 'n':
			case 's':
				$this->data['search_type'] = 'main_goods';
				$this->data['search'] = $this->Main_goods_model->search($type, $search);
				break;
			case 'sn':
			case 'ss':
				$this->data['search_type'] = 'sub_goods';
				$this->data['search'] = $this->Sub_goods_model->search($type, $search);
				break;
			default:
				return false;
				break;
		}
		$this->data['list']['opt'] = 'all';
		return $this->load->view_('goods_search_view', $this->data);
	}

	//Ajax function
	public function ajax_search($type, $keyword)
	{
		switch ($type)
		{
			case 'n':
			case 's':
				$this->data['ajax']['data'] = $this->Main_goods_model->search($type, $keyword, 0);
				break;
			case 'sn':
			case 'ss':
				$this->data['ajax']['data'] = $this->Sub_goods_model->search($type, $keyword, 0);
				break;
			default:
				return false;
				break;
		}
		$this->data['ajax']['request'] = 'SEARCH_DONE';
		return $this->load->view('ajax_json', $this->data);
	}

	public function ajax_get_sub_goods($main_goods_id)
	{
		if ($this->Main_goods_model->getOne($main_goods_id))
		{
			$this->data['ajax']['data'] = $this->Sub_goods_model->getGoods($main_goods_id, 0);
			$this->data['ajax']['request'] = 'GET_SUB_DONE';
			$this->load->view('ajax_json', $this->data);
		}
		else
		{
			$this->data['ajax']['request'] = 'GET_SUB_FAILED';
			$this->load->view('ajax_request', $this->data);
		}
	}

	//Get some sub goods infomation by using AJAX
	public function ajax_get_some_sub_goods()
	{
		$this->data['some_sub_goods'] = $this->Sub_goods_model->get_some();
		if (empty($this->Sub_goods_model->error))
		{
			$this->data['ajax']['request'] = 'GET_DONE';
			$this->data['ajax']['data'] = json_encode($this->data['some_sub_goods']);
		}
		else
		{
			$this->data['ajax']['request'] = 'ERROR';
			$this->data['ajax']['data'] = $this->Sub_goods_model->error;
		}
		$this->load->view('ajax_json', $this->data);
	}

	//同步淘宝基本信息
	public function sync_taobao()
	{
		$this->data['ajax_request'] = $this->Main_goods_model->sync_taobao();
		if (empty($this->Main_goods_model->error))
		{
			$this->data['ajax']['request'] = 'SYNC_DONE';
			$this->data['ajax']['data'] = $this->Main_goods_model->data;
		}
		else
		{
			$this->data['ajax']['request'] = 'SYNC_ERROR';
			$this->data['ajax']['data'] = $this->Main_goods_model->error;
		}
		$this->load->view('ajax_json', $this->data);
	}

	//生成所有宝贝信息
	public function show_table()
	{
		$this->data['goods'] = $this->Main_goods_model->getList('enable', 0);
		$this->load->view('Show_table_view', $this->data);
	}

}